package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.dao.mapper.ShoppingCartMapper;
import com.ddwl.goods.dao.model.ShoppingCart;
import com.ddwl.goods.dto.ShoppingCarItemNumDto;
import com.ddwl.schema.bo.goods.retail.ShoppingCartPageByShopBo;
import com.ddwl.schema.vo.goods.retail.ShoppingCartPageVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * (SysShoppingCart)表数据库访问层
 *
 * @author makejava
 * @since 2020-06-05 16:01:26
 */
@Repository
public class ShoppingCartDao extends TkDao<ShoppingCartMapper, ShoppingCart> {

    @Resource
    private ShoppingCartMapper sysShoppingCartMapper;


    public int deleteByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }
        Example example = new Example(ShoppingCart.class);
        example.and().andIn("id", ids)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        ShoppingCart sysShoppingCart = new ShoppingCart();
        sysShoppingCart.setGmtModified(Instant.now());
        sysShoppingCart.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        return sysShoppingCartMapper.updateByExampleSelective(sysShoppingCart, example);
    }

    /**
     * 查询用户购物车sku详情
     *
     * @param skuCode
     * @param userId
     * @return
     */
    public ShoppingCart selectBySkuCode(String skuCode, String userId) {
        ShoppingCart sysShoppingCart = new ShoppingCart();
        sysShoppingCart.setSkuCode(skuCode);
        sysShoppingCart.setUserId(userId);
        return selectOne(sysShoppingCart);
    }

    public List<ShoppingCart> selectBySkuCodes(List<String> skuCodes, String userId) {
        Example example = new Example(ShoppingCart.class);
        example.and().andIn("skuCode", skuCodes)
                .andEqualTo("userId", userId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return sysShoppingCartMapper.selectByExample(example);
    }


    /**
     * 清空用户购物车
     *
     * @param userId
     */
    public void clearUserCard(String userId) {
        Example example = new Example(ShoppingCart.class);
        ShoppingCart where = new ShoppingCart();
        where.setUserId(userId);
        where.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        example.and().andEqualTo(where);
        ShoppingCart sysShoppingCart = new ShoppingCart();
        sysShoppingCart.setGmtModified(Instant.now());
        sysShoppingCart.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        sysShoppingCartMapper.updateByExampleSelective(sysShoppingCart, example);
    }


    /**
     * 查询用户购物车
     *
     * @param userId
     */
    public List<ShoppingCart> queryUserShoppingCart(String userId) {
        ShoppingCart where = new ShoppingCart();
        where.setUserId(userId);
        return select(where);
    }

    public List<ShoppingCartPageVo> getListGroupByShop(String userId) {
        return sysShoppingCartMapper.getListGroupByShop(userId);
    }

    public List<ShoppingCart> getListByShop(ShoppingCartPageByShopBo param) {
        ShoppingCart where = new ShoppingCart();
        where.setUserId(param.getUserId());
        where.setShopId(param.getShopId());
        return select(where);
    }

    public List<ShoppingCarItemNumDto> getItemNumGroupByItem(String userId, List<String> itemIds) {
        return sysShoppingCartMapper.getItemNumGroupByItem(userId, itemIds);
    }

    public List<ShoppingCarItemNumDto> getItemNumGroupByBarcode(String userId, List<String> barcodes) {
        return sysShoppingCartMapper.getItemNumGroupByBarcode(userId, barcodes);
    }

    public List<ShoppingCart> getListByUserIdAndShop(List<String> shopList, String userId) {
        Example example = getExample();
        example.and().andEqualTo("userId", userId)
                .andIn("shopId", shopList);
        return sysShoppingCartMapper.selectByExample(example);
    }

    public Integer count(String userId) {
        ShoppingCart entity = new ShoppingCart();
        entity.setUserId(userId);
        entity.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return sysShoppingCartMapper.selectCount(entity);
    }
}